<?php

namespace App\Admin\Controllers;

use App\Models\Course;
use App\Models\CourseOrder;
use App\Models\Estimate;
use App\Models\Teacher;
use App\Models\Users;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;


class HomeController extends BaseController
{
    public function index(Content $content)
    {
        Admin::disablePjax();
//        return $content ->title('首页');
        $staticsData = self::topStatics();
        return $content
            ->header(" ")
            ->description(' ')
            ->body(view('admin.home.index', compact("staticsData")));
    }

    protected static function topStatics()
    {
        $course         = Estimate::select('course_id')->selectRaw('AVG(course_score) as average_course_score')->groupBy('course_id')->orderBy('average_course_score', 'desc')->first();
        $maxCourse      = Course::where('id', $course['course_id'])->value('name');
        $teacher        = Estimate::select('teacher_id')->selectRaw('AVG(teacher_score) as average_teacher_score')->groupBy('teacher_id')->orderBy('average_teacher_score', 'desc')->first();
        $maxTeacher     = Teacher::where('id', $teacher['teacher_id'])->value('name');
        $user           = Users::orderBy('score', 'desc')->first();
        $maxScoreUser   = $user['nickname'];
        $userCount      = Users::count();
        $study          = CourseOrder::where('status', '!=', -1)->where('status', '!=', 1)->where('status', '!=', 4)->selectRaw('COUNT(DISTINCT(user_id)) as study_count')->first();
        $userStudyCount = $study['study_count'];
        $teacherCount   = Teacher::where('status', 1)->count();
        $courseCount    = Course::where('valid', 1)->count();
        return compact("maxCourse", "maxTeacher", "maxScoreUser", "userCount", "userStudyCount", "teacherCount", "courseCount");
    }

}
